# liner regression
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

tf.set_random_seed(777)

# datasets
xy = np.loadtxt('data-01-test-score.csv', delimiter=',', dtype=np.float32)

x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]

# placeholder
X = tf.placeholder(tf.float32, shape=[None, 3])
Y = tf.placeholder(tf.float32, shape=[None, 1])

# model
W = tf.Variable(tf.random_normal([3, 1]), name="Weight")
b = tf.Variable(tf.random_normal([1]), name="bias")

h = tf.matmul(X, W) + b

# cost
cost = tf.reduce_mean(tf.square(h - Y))
cost_history = []

# Grandien Descent
e = h - Y
dW = tf.matmul(tf.transpose(X), e) / tf.cast(tf.shape(X)[0], tf.float32)
db = tf.reduce_mean(e, axis=[0])

# update
learning_rate = 10e-5
update = [
    tf.assign(W, W - learning_rate * dW),
    tf.assign(b, b - learning_rate * db)
]

# launch a session
sess = tf.Session()
with sess:
    sess.run(tf.global_variables_initializer())

    # train model
    for step in range(4001):
        cost_val, update_val = sess.run([cost, update], feed_dict={X: x_data, Y: y_data})

        if step % 200 == 0:
            print(("Step: ", step, "Cost: ", cost_val, "update_val:", update_val))
            cost_history.append(cost_val)

    # plot
    plt.plot(cost_history[1:])
    plt.show()

    print('theta:', b.eval(), W.eval())

    # Ask my score
    print("Your score will be ", sess.run(h, feed_dict={X: [[100, 70, 101]]}))

    print("Other scores will be ", sess.run(h,
                                            feed_dict={X: [[60, 70, 110], [90, 100, 80]]}))
